import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
interface Message {
	filter: {
		hidden_source_feature_no: string;
		pn: number;
	};
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
/* 表名	modify_record
标题	修改记录
字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
_id	id	string	50	ⅹ	√
change_personnel	更改人	string	50	√	ⅹ
hidden_source_feature_no	隐患源特性编号	string	50	√	ⅹ
change_date	更改时间	timestamp	50	√	ⅹ
change_field	更改字段	string	32	√	ⅹ
change_before	更改前	string	32	√	ⅹ
change_after	更改后	string	32	√	ⅹ
 */
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf\zj-000006\s001,action_id:' + action_id);

	const q = query(action_id, session_id, systemid);

	q.prepare('modify_record', ['change_personnel', 'hidden_source_feature_no', 'change_date', 'change_field', 'change_before', 'change_after'], { productid, hidden_source_feature_no: message.filter.hidden_source_feature_no }, 2, message.filter.pn, [], []);

	const [res] = await q.exec<{ change_date: number; }>();
	const data = res.map((value) => {
		const date = format(value.change_date, 'YYYY-MM-DD');
		return { ...value, change_date: date };
	});

	const { prepare, exec } = count(action_id, session_id, systemid);/*(给prepare起了别名p)*/
	prepare('modify_record', { productid, hidden_source_feature_no: message.filter.hidden_source_feature_no });
	const [c] = await exec();

	log('Service end path:f:\实习\kh\src\wyf\zj-000006\s001,action_id:' + action_id);
	return {
		data: { data, c }
	};
}
